package service

import (
	"ksd-social-api/modules/note/model/vo"
)

type NoteCategoryService struct{}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  查询所有的发布的和未删除的小册分类信息
 */
func (service *NoteCategoryService) FindCategories(channelId uint64, systemId uint64) ([]*vo.NoteCategoryVo, error) {
	var noteVos []*vo.NoteCategoryVo
	categoryList, _ := noteCategoryMapper.FindCategories(channelId, systemId)
	for _, category := range categoryList {
		noteVo := vo.NoteCategoryVo{category.Id, category.Title, category.Icon, category.ParentId, category.ChannelId, []*vo.NoteCategoryVo{}, category.ChannelIds}
		noteVos = append(noteVos, &noteVo)
	}
	return service.Tree(noteVos, 0), nil
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  查询所有的发布的和未删除的小册分类信息
 */
func (service *NoteCategoryService) FindCategoriesAll(systemId uint64) ([]*vo.NoteCategoryVo, error) {
	var noteVos []*vo.NoteCategoryVo
	categoryList, _ := noteCategoryMapper.FindCategoriesAll(systemId)
	for _, category := range categoryList {
		noteVo := vo.NoteCategoryVo{category.Id, category.Title, category.Icon, category.ParentId, category.ChannelId, []*vo.NoteCategoryVo{}, category.ChannelIds}
		noteVos = append(noteVos, &noteVo)
	}
	return service.Tree(noteVos, 0), nil
}

/**
*   开始把数据进行编排--递归
*   Tree(all,0)
 */
func (service *NoteCategoryService) Tree(allList []*vo.NoteCategoryVo, parentId uint64) []*vo.NoteCategoryVo {
	var nodes []*vo.NoteCategoryVo
	for _, dbMenu := range allList {
		if dbMenu.ParentId == parentId {
			childrensMenu := service.Tree(allList, dbMenu.Id)
			if len(childrensMenu) > 0 {
				dbMenu.Children = append(dbMenu.Children, childrensMenu...)
			}
			nodes = append(nodes, dbMenu)
		}
	}
	return nodes
}
